/**
  ******************************************************************************
  * @file    :Main.c
  * @author  :MG Team
  * @version :V1.0
  * @date
  * @brief   :for extended adv and AoA
  ******************************************************************************
***/

/* Includes ------------------------------------------------------------------*/
#include "Includes.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
extern unsigned short tick;
extern void BLE_TX_Ext(void);
extern void update_adv_ext_ind_data(unsigned char* data, unsigned char len);
extern void update_aux_chain_ind_data(unsigned char* data, unsigned char len);

extern u8 g_pkt_types;
extern u8  g_adv_data_pkt2[];
extern u8  g_adv_data_pkt2_len;
extern u8  g_adv_data_pkt3[];
extern u8  g_adv_data_pkt3_len;


/* Private function prototypes -----------------------------------------------*/
void BLE_RESET(void)
{
    GPIO_ResetBits(BLE_nRST_PORT,BLE_nRST);
    Delay_ms(2);
    GPIO_SetBits(BLE_nRST_PORT,BLE_nRST);
    Delay_ms(20);
#if (COMM_OPT_SEL == COMM_I2C_SIMU_NOPU)
    GPIO_Init(BLE_SCL_PORT,BLE_SCL,GPIO_Mode_Out_PP_High_Slow);
#endif
}

/*******************************************************************************
* Function	 :		main
* Parameter  :		void
* Returns	 :		void
* Description:
* Note: 	 :
*******************************************************************************/
void main(void)
{
    static uint8_t flag_pressed = 0;
    u8 update_adv_data_flag = 0;
    u8 irq_status = 0;

    //init mcu system
    Init_System();

    LED_RED_ON();
    LED_GREEN_ON();
    Delay_ms(20);

#if (POWER_SAVE_OPT == POWER_SAVE_STANDBY)
    BLE_RESET();
#endif

    ////BLE initial
    BLE_Init();
    Uart_Send_String("BLE init ok.\r\n");

# if 0 //carrier test
    SRCC_CarrierTest(19); //0:2402, 19:2440, 39:2480
    Uart_Send_String("carrier testing 2440...");

    while(1)
    {
        if(KEY_GET() == RESET)
        {
            LED_RED_OFF();
           //push button to exit carrier mode
            MG_Write_Reg(BASEBAND_CFG, 2); //STOP=2
            break;
        }
        else
        {
            LED_RED_ON();
            flag_pressed = 0;
        }
    }

    BLE_Init();
    Uart_Send_String("BLE tx demo.\r\n");
#endif

    //////ble tx api
    BLE_TX_Ext();
    
    tick = 5000; //tx 3s
    
    while(1)
    {
        LED_GREEN_OFF();
        
        if(RESET == BLE_IRQ_GET())
        {
            irq_status = radio_getIrqState();
            
            //if(irq_status & (0x01|0x04|0x10))
            {
                LED_GREEN_ON(); //debug
            }
            
            
            if(irq_status & 0x04) //pkt2
            {
                if(g_pkt_types & AUX_ADV_IND_BIT)
                {
                    update_adv_ext_ind_par();
                }
                if(g_pkt_types & AUX_CHAIN_IND_BIT)
                {
                    update_aux_chain_ind_par();
                }

                //update pkt2 if changed
                if(update_adv_data_flag & 0x01)
                {
                    update_adv_ext_ind_data(g_adv_data_pkt2, g_adv_data_pkt2_len);
                    update_adv_data_flag &= 0xfe;
                }
                
            }
            
            if(irq_status & 0x01) //pkt3
            {//update pkt3 if changed
                if(update_adv_data_flag & 0x04)
                {
                    update_aux_chain_ind_data(g_adv_data_pkt3, g_adv_data_pkt3_len);
                    update_adv_data_flag &= 0xfb; 
                }
            }
        }

#if 1
        //key test demo
        if(KEY_GET() == RESET)
        {
            if(flag_pressed == 0){
                flag_pressed = 1;
                g_adv_data_pkt3[g_adv_data_pkt3_len-1]++;
                update_adv_data_flag = 0x04; //pkt3 data changed
            }
            LED_RED_OFF();

        }else {
            flag_pressed = 0;
            LED_RED_ON();

        }
#endif
    }
}
